package com.kaibes.module.permission.api;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.module.permission.db.entity.Role;
import com.kaibes.module.permission.db.service.RoleService;
import com.kaibes.module.permission.pojo.RolePost;
import com.kaibes.module.permission.pojo.RoleUpdate;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;

@RestController
@RequestMapping("/roles")
public class RoleApi extends ResponseApi<Role, RoleService> {

    @PostMapping
    @ApiLink("创建角色")
    public ResponseEntity<List<Number>> post(@RequestBody @Validated RolePost role) {
        if (role.getSort() == null) {
            role.setSort(0);
        }
        return super.post(role, "创建角色成功", "创建角色失败");
    }

    @DeleteMapping
    @ApiLink("删除角色，根据id")
    public ResponseEntity<Object> delete(@RequestBody List<Integer> ids) {
        return super.deleteByIds(ids, "删除角色成功", "删除角色失败");
    }
    
    @DeleteMapping("/{id:\\d+}")
    @ApiLink("删除角色，根据id")
    public ResponseEntity<Object> delete(@PathVariable Integer id) {
        return super.deleteById(id, "删除角色成功", "删除角色失败");
    }

    @PutMapping("/{id:\\d+}")
    @ApiLink("修改角色，根据id")
    public ResponseEntity<Object> putById(@PathVariable Integer id, @RequestBody @Validated RoleUpdate role) {
        return super.putById(role, id, "更新角色信息成功", "更新角色信息失败");
    }

}
